<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-left">
          <img src="../../assets/logo.png">
          <span>欢迎来到德莱联盟</span>
        </div>
        <el-button @click="loginOut">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="flag ? '64px' : '200px'">
          <div class="toggleMenu" @click="flag = !flag">|||</div>
          <!-- 第一个 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            :unique-opened="true"
            :collapse="flag"
            :collapse-transition="false"
            :router="true"
            :default-active="$route.path"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span> {{ item.authName }} </span>
              </template>
              <el-menu-item
                :index="'/' + item1.path"
                v-for="item1 in item.children"
                :key="item1.id"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span> {{ item1.authName }} </span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      flag: false
    }
  },

  created () {
    this.getMenuList()
  },

  methods: {
    loginOut () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message(res.meta.msg)
      this.menuList = res.data
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  height: 100%;
  .el-container {
    height: 100%;
  }
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-aside {
    background-color: #333744;
    .iconfont {
      margin-right: 10px;
    }
    .el-menu {
      border-right: none;
    }
    .toggleMenu {
      width: 100%;
      height: 40px;
      line-height: 40px;
      text-align: center;
      background-color: orange;
      color: #fff;
      letter-spacing: 5px;
      cursor: pointer;
      user-select: none;
    }
  }
  .el-main {
    background-color: #eee;
  }
  .el-button {
    height: 40px;
  }
}
.header-left {
  display: flex;
  align-items: center;
  span {
    font-size: 20px;
    color: #fff;
    margin-left: 10px;
  }
  span {
    user-select: none !important;
  }
}
</style>
